package com.glq.book.dao;

import com.glq.book.entity.User;
import com.glq.book.util.JdbcUtil;
import com.glq.book.vo.LoginVO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserDao {

    private JdbcUtil jdbcUtil = new JdbcUtil();
    private Connection connection = null;
    private ResultSet resultSet = null;
    private PreparedStatement preparedStatement = null;
    private String sql = "";

    /**
     * 获取所有用户（管理员）
     *
     * @return
     */
    public List<User> selectUserList() {
        List<User> list = new ArrayList<>();
        try {
            sql = "select * from `user` order by username DESC ";
            connection = JdbcUtil.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                list.add(new User(resultSet.getInt(1),
                        resultSet.getString(2),
                        resultSet.getString(3),
                        resultSet.getString(4),
                        resultSet.getInt(5),
                        resultSet.getString(6),
                        resultSet.getString(7)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.closeConnection(connection, preparedStatement, resultSet);
        }

        return list;
    }

    /**
     * 获取个人信息
     *
     * @param username
     * @return
     */
    public User selectUserByUserName(String username) {
        User user = new User();
        try {
            sql = "select * from `user` where username = ?";
            connection = JdbcUtil.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                user = new User(resultSet.getInt(1),
                        resultSet.getString(2),
                        null,
                        resultSet.getString(4),
                        resultSet.getInt(5),
                        resultSet.getString(6),
                        resultSet.getString(7));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.closeConnection(connection, preparedStatement, resultSet);
        }
        return user;
    }


    /**
     * 登陆
     *
     * @param user
     * @return
     */
    public User login(LoginVO user) {
        User loginUser = null;
        try {
            sql = "select * from `user` where username = ? and password = ?";
            connection = JdbcUtil.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getUsername());
            preparedStatement.setString(2, user.getPassword());
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                loginUser = new User(resultSet.getInt(1),
                        resultSet.getString(2),
                        null,
                        resultSet.getString(4),
                        resultSet.getInt(5),
                        resultSet.getString(6),
                        resultSet.getString(7));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.closeConnection(connection, preparedStatement, resultSet);
        }
        return loginUser;
    }

    /**
     * 注册
     *
     * @param user
     * @return
     */
    public boolean register(LoginVO user) {
        try {
            sql = "insert into `user`(username,password,touxiang) values(?,?,?)";
            connection = JdbcUtil.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getUsername());
            preparedStatement.setString(2, user.getPassword());
            preparedStatement.setString(3,"/myimger/3.jpg");
            return preparedStatement.execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.closeConnection(connection, preparedStatement, resultSet);
        }
        return true;
    }

    /**
     * 修改个人信息
     *
     * @param user
     * @return
     */
    public boolean updateUserInfoByUsername(User user) {
        try {
            sql = "update `user` set true_name=?,age=?,tel=? where username=?";
            connection = JdbcUtil.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getTrueName());
            preparedStatement.setInt(2, user.getAge());
            preparedStatement.setString(3, user.getTel());
            preparedStatement.setString(4, user.getUsername());
            return preparedStatement.execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.closeConnection(connection, preparedStatement, resultSet);
        }
        return true;
    }

    /**
     * 修改密码
     *
     * @param user
     * @return
     */
    public boolean updatePasswordByUsername(LoginVO user) {
        try {
            sql = "update `user` set password=? where username=?";
            connection = JdbcUtil.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getPassword());
            preparedStatement.setString(2, user.getUsername());
            return preparedStatement.execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.closeConnection(connection, preparedStatement, resultSet);
        }
        return true;
    }

    public void updateUserTouXiang(String filePath, String username) {
        try {
            sql = "update `user` set touxiang = ? where username = ?";
            connection = JdbcUtil.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, filePath);
            preparedStatement.setString(2, username);
            preparedStatement.execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.closeConnection(connection, preparedStatement, resultSet);
        }
    }
}
